package com.wuhao.code.check.completion

import com.aegis.mybatis.xmlless.annotations.XmllessMethod
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.psi.PsiReferenceProvider
import com.intellij.psi.PsiReferenceRegistrar
import com.intellij.psi.util.parentOfType
import com.intellij.util.ProcessingContext
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.KtValueArgument

/**
 * @author 吴昊
 * @date 2024/02/01
 * @version 1.0
 * @since v1.0.0
 */
class KotlinReferenceContributor : BaseXmllessReferenceContributor() {

  override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
    registrar.registerReferenceProvider(
        psiElement(KtStringTemplateExpression::class.java),
        object : PsiReferenceProvider() {

          override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> {
            val arg = element.parentOfType<KtValueArgument>() ?: return PsiReference.EMPTY_ARRAY
            if (arg.firstChild.text != "conditions") {
              return PsiReference.EMPTY_ARRAY
            }
            val anno = element.parentOfType<KtAnnotationEntry>()
            if (anno?.shortName?.asString() != XmllessMethod::class.java.simpleName) {
              return PsiReference.EMPTY_ARRAY
            }
            return create(element.text, element)
          }

        }
    )
  }

}
